﻿//-----------------------------------------------------------------------
// <copyright file="CellHelper.cs" company="cx studio">
//     Copyright (c) cx studio. All rights reserved.
// </copyright>
// <author>Xi Chen</author>
//-----------------------------------------------------------------------
namespace CentralFoveaLib.Helper
{

    using CentralFoveaLib.Brain;
    using CentralFoveaLib.ColorProcess;
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 用来处理Cell
    /// </summary>
    public class CellHelper
    {
        public static Cell GetCell(List<Cell> cells, int x, int y)
        {
            for (int i = 0; i < cells.Count; i++)
            {
                var cell = cells[i];
                if (cell.X == x && cell.Y == y)
                {
                    return cell;
                }

            }
            return null;
        }

        public static List<CCell> PGroup(List<CCell> cells)
        {
            double th =0 ;
            for (int i = 1; i < cells.Count; i++)
            {
                th+= cells[i].Gray;
                
            }
            th = th / cells.Count;
            for (int i = 0; i < cells.Count; i++)
            {
                var cell = cells[i];
                if (cell.Gray >= th) cell.L = true;
            }
            return cells;
        }

        /// <summary>
        /// 根据颜色分组
        /// </summary>
        /// <param name="cells">格子的集合</param>
        /// <returns>分组后的颜色</returns>
        public static IDictionary<int, List<CCell>> GetGroup(List<CCell> cells)
        {
            IDictionary<int, List<CCell>> dict = new Dictionary<int, List<CCell>>();
            foreach (var item in cells)
            {
                int hslLevel = 0;
                if (item.RGB.R == item.RGB.G && item.RGB.R == item.RGB.B)
                {
                    hslLevel = (int)Math.Floor(item.Gray / 16);
                }
                else
                {
                    hslLevel = ColorUtils.GetColorName(item.HSL) + 16;
                }

                List<CCell> cellItem = null;
                if (dict.ContainsKey(hslLevel))
                {
                    cellItem = dict[hslLevel];
                }
                else
                {
                    cellItem = new List<CCell>();
                    dict[hslLevel] = cellItem;
                }
                cellItem.Add(item);
            }
            return dict;
        }

        /// <summary>
        ///  根据灰度将颜色分组
        /// </summary>
        /// <param name="cells">格子的集合</param>
        /// <returns>分组后的颜色</returns>
        public static IDictionary<int, List<CCell>> GetGrayGroup(List<CCell> cells)
        {
            IDictionary<int, List<CCell>> dict = new Dictionary<int, List<CCell>>();
            foreach (var item in cells)
            {

                int hslLevel = (int)Math.Floor(item.Gray / 16);


                List<CCell> cellItem = null;
                if (dict.ContainsKey(hslLevel))
                {
                    cellItem = dict[hslLevel];
                }
                else
                {
                    cellItem = new List<CCell>();
                    dict[hslLevel] = cellItem;
                }
                cellItem.Add(item);
            }
            return dict;
        }
    }
}
